package glarge

import (
	"glarge/config"
	mylogfile "glarge/file"
)

/*
	eg
	1、当日志界别为4， 其他输出都会在其对应的日志路径
	2、当日志界别为1， 其他输出都会在sys.log

*/

// 日志级别
const (
	//LOGLEVELDEBUG is normal
	loglevldebug = 1
	//LOGLEVELNORMAL is normal
	loglevlinfo = 2
	//LOGLEVELWARRNING is normal
	loglevelwarrning = 3
	//LOGLEVELERROR is normal
	loglevelerror = 4
)

// Glarge is struct
//	一般来说不要把strcut暴露到外面
//type glarge struct {
type Glarge struct {
	logLevel     int
	filePath     string
	debugPath    string
	infoPath     string
	warrningPath string
	errorPath    string
}

// DebugMsg 调试日志
func (glarge *Glarge) DebugMsg(format string, args ...interface{}) {
	filePath := getPath1(loglevldebug, glarge.logLevel, glarge.debugPath, glarge.filePath)
	mylogfile.WriteFile("DEBUG", filePath, format, args)
	mylogfile.WriteFile("DEBUG", glarge.debugPath, format, args)
}

// InfoMsg 信息输出日志
func (glarge *Glarge) InfoMsg(format string, args ...interface{}) {
	filePath := getPath1(loglevlinfo, glarge.logLevel, glarge.infoPath, glarge.filePath)
	mylogfile.WriteFile("INFO", filePath, format, args)
	mylogfile.WriteFile("INFO", glarge.infoPath, format, args)
}

// WarrnMsg 警告输出日志
func (glarge *Glarge) WarrnMsg(format string, args ...interface{}) {
	filePath := getPath1(loglevelwarrning, glarge.logLevel, glarge.warrningPath, glarge.filePath)
	mylogfile.WriteFile("WARRNING", filePath, format, args)
	mylogfile.WriteFile("WARRNING", glarge.warrningPath, format, args)
}

// ErrorMsg 错误输出日志
func (glarge *Glarge) ErrorMsg(format string, args ...interface{}) {
	filePath := getPath1(loglevelerror, glarge.logLevel, glarge.errorPath, glarge.filePath)
	mylogfile.WriteFile("ERROR", filePath, format, args)
	mylogfile.WriteFile("ERROR", glarge.errorPath, format, args)
}

func getPath1(funLevel, logLevel int, funPath, logPath string) string {
	if funLevel > logLevel {
		return logPath
	} else {
		return funPath
	}
}

// NewtGlarge is a func
func NewtGlarge() (glarge *Glarge, err error) {
	// 1 读取配置
	var iniConfig config.IniConfig
	gLargeConfigOjb, err := config.GenConfig1(&iniConfig)
	if err != nil {
		return nil, nil
	} else {
		return &Glarge{
			debugPath:    gLargeConfigOjb.DebugFile,
			infoPath:     gLargeConfigOjb.InfoFile,
			warrningPath: gLargeConfigOjb.WarrningFile,
			errorPath:    gLargeConfigOjb.ErrorFile,
			filePath:     gLargeConfigOjb.SysFile,
			logLevel:     gLargeConfigOjb.LogLevel,
		}, nil
	}
}
